package com.adinnet.admin.teacher.service.impl;

import com.adinnet.admin.teacher.mapper.TeacherMapper;
import com.adinnet.admin.teacher.model.Teacher;
import com.adinnet.admin.teacher.model.form.TeacherForm;
import com.adinnet.admin.teacher.model.vo.TeacherReq;
import com.adinnet.admin.teacher.model.vo.TeacherResp;
import com.adinnet.admin.teacher.service.TeacherService;
import com.adinnet.admin.user.mapper.UserMapper;
import com.adinnet.admin.user.model.User;
import com.adinnet.common.sms.response.SmsSendResponse;
import com.adinnet.common.sms.util.ChuangLanSmsUtil;
import com.adinnet.common.utils.MD5Util;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 儿童表 服务实现类
 * </p>
 *
 * @author chenjia
 * @since 2021-06-09
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    private Logger log = LoggerFactory.getLogger("ChildServiceImpl");


    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public PageResult<TeacherResp> queryTeacherList(TeacherForm teacherForm) {
        Page<TeacherResp> page = new Page<>(teacherForm.getPage(), teacherForm.getLimit());
        List<TeacherResp> list = baseMapper.queryTeacherList(page, teacherForm);
        return new PageResult<>(list, teacherForm.getTotal());
    }

    @Override
    @Transactional
    public Integer addNewTeacher(TeacherReq teacherReq) {
        String psw = MD5Util.MD5("a123456", "UTF-8");
        User user = new User();
        Teacher c = new Teacher();
        BeanUtils.copyProperties(teacherReq,user);
        BeanUtils.copyProperties(teacherReq,c);
        user.setPwd(psw);
        user.setType(2);
        userMapper.insert(user);
        //初始的账号默认的段位为1
        c.setUserId(user.getId());
        c.setPeaseLevelId(1);
        c.setRankLevelId(1);
        teacherMapper.insert(c);
        if(c.getId()!=null) {
            String msg = "亲爱的用户，您的默认登陆密码为：a123456，感谢注册！";
            SmsSendResponse smsSendResponse = ChuangLanSmsUtil.sendCommonSms(teacherReq.getTelNo(), msg);
            if (!smsSendResponse.getCode().equals("0")) {
                //短信经常会因为ip限制和其他外部原因发送失败，这里短信发送失败还是让正常注册，不影响流程
                //throw new BizException("注册失败,发送短信失败");
                log.info("用户{}发送注册短信失败",user.getId());
            }
        }
        return c.getId();
    }

    @Override
    @Transactional
    public Integer editTeacher(TeacherReq teacherReq) {
        String psw = MD5Util.MD5("a123456", "UTF-8");
        Teacher teacher = teacherMapper.selectById(teacherReq.getChildId());
        User user = userMapper.selectById(teacher.getUserId());
        //新建修改实体,不用再插入userId
        User u = new User();
        Teacher c = new Teacher();
        BeanUtils.copyProperties(teacherReq,u);
        BeanUtils.copyProperties(teacherReq,c);
        u.setId(user.getId());
        c.setId(teacher.getId());
        if(!user.getTelNo().equals(teacherReq.getTelNo())){
            User ou=userMapper.selectOne(new QueryWrapper<User>().eq("tel_no",teacherReq.getTelNo()).eq("deleted", 0));
            if(ou!=null){
                throw new BizException("编辑失败,手机号已存在注册用户");
            }
            u.setPwd(psw);
            String msg = "亲爱的用户，您的登陆手机号已变更，默认登陆密码重置为：a123456，，感谢使用！";
            SmsSendResponse smsSendResponse = ChuangLanSmsUtil.sendCommonSms(teacherReq.getTelNo(), msg);
            if (!smsSendResponse.getCode().equals("0")) {
                //throw new BizException("编辑失败,发送短信失败");
                log.info("用户{}发送变更注册手机号短信失败",user.getId());
            }
        }
        userMapper.updateById(u);
        teacherMapper.updateById(c);
        return c.getId();
    }

    @Override
    public JsonResult deleteIds(String teacherIds) {
        teacherMapper.deleteIds(teacherIds);
        userMapper.deleteChilds(teacherIds);
        return JsonResult.ok("操作成功");
    }

    @Override
    public List<TeacherResp> getTeacherList() {
        TeacherForm teacherForm=new TeacherForm();
        teacherForm.setSize(999);
        List<TeacherResp> list = baseMapper.queryTeacherList(null, teacherForm);
        return list;
    }
}
